﻿using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using NPOI.SS.Formula.Atp;
using SmartWaterMointerCenter.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO.Ports;


namespace SmartWaterMointerCenter.Utils
{
    public class CommonHelper
    {
        // 配置文件存储目录路径
        public static string configFilePath = Application.StartupPath + "/files";
        public static string slavePath = configFilePath + "/XSlaves.xml";
        public static string floorPath = configFilePath + "/Floors.json";
        public static string connectPath = configFilePath + "/Communicate.txt";
        public static string levelPath = configFilePath + "/WMLevelSets.xml";
        public static string residentPath = configFilePath + "/Residents.json";
        public static List<SlaveInfo> slaveList = new List<SlaveInfo>();// 从站列表
        public static List<FloorInfo> floorList = new List<FloorInfo>();// 楼层列表
        public static List<LevelInfo> levelList = new List<LevelInfo>();// 等级列表
        public static List<ResidentInfo> residentList = new List<ResidentInfo>(); //住户列表
        public static SerialPort serialPort = null;//主站串口
        public static bool IsFloorUpdated = false; // 楼层信息更新标识
        public static bool IsResidentUpdated = false;//住户信息是否更新
        public static void LoadSlaveList()
        {
            XmlDocument doc = new XmlDocument();
            if (File.Exists(slavePath))
            {
                doc.Load(slavePath);
                XmlElement rootNode = doc.DocumentElement;
                if (rootNode != null)
                {
                    foreach (XmlNode node in rootNode.ChildNodes)
                    {
                        SlaveInfo slaveInfo = new SlaveInfo();
                        slaveInfo.SlaveId = node.SelectSingleNode("SlaveId").InnerText.GetByte();
                        slaveInfo.FunctionCode = node.SelectSingleNode("FunctionCode").InnerText.GetByte();
                        slaveInfo.StartAddress = node.SelectSingleNode("StartAddress").InnerText.GetUshort();
                        slaveInfo.Count = node.SelectSingleNode("Count").InnerText.GetUshort();
                        slaveList.Add(slaveInfo);
                    }
                }
            }
        }

        public static void LoadFloorList()
        {
            if (File.Exists(floorPath))
            {
                string json = File.ReadAllText(floorPath);
                if (!string.IsNullOrEmpty(json))
                {
                    floorList = JsonConvert.DeserializeObject<List<FloorInfo>>(json);//  反序列化
                }
            }
        }

        public static void CreateConn()
        {

            string portName = "";
            int baudRate = 0;
            Parity parity = Parity.None;
            int dataBits = 8;
            StopBits stopBits = StopBits.None;
            if (File.Exists(connectPath))
            {
                string connectStr = File.ReadAllText(connectPath);
                if (!string.IsNullOrEmpty(connectStr))
                {
                    string[] arr = connectStr.Split(";");
                    portName = arr[0];
                    baudRate = arr[1].GetInt();
                    dataBits = arr[2].GetInt();
                    stopBits = Enum.Parse<StopBits>(arr[3]);
                    parity = Enum.Parse<Parity>(arr[4]);
                    serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
                }
            }
        }


        public static void LoadLevelList() {
            levelList.Clear();
            XmlDocument doc = new XmlDocument();
            if (File.Exists(levelPath))
            {
                doc.Load(levelPath);
                XmlElement rootNode = doc.DocumentElement;
                if (rootNode != null)
                {
                    foreach (XmlNode node in rootNode.ChildNodes)
                    {
                        LevelInfo level = new LevelInfo();
                        //TODO: SelectSingleNode和 Attributes 区别
                        level.Grade = node.Attributes["grade"].Value.GetInt();
                        level.Min = node.SelectSingleNode("Min").InnerText.GetDecimal();
                        //level.Grade = node.Attributes["grade"].Value.GetInt();
                        if (node.SelectSingleNode("Max") != null) 
                        {
                            level.Max = node.SelectSingleNode("Max").InnerText.GetDecimal();
                        }
                        levelList.Add(level);
                    }
                }
            }
        }

        public static void LoadResidentList() {

            if (File.Exists(residentPath))
            {
                string json = File.ReadAllText(residentPath);
                if (!string.IsNullOrEmpty(json))
                {
                    residentList = JsonConvert.DeserializeObject<List<ResidentInfo>>(json);//反序列化
                }
            }
        }
    
    }
}
